<?php
/**
*  Create On 2010-7-12
*  Author Been
*  QQ:281443751
*  Email:binbin1129@126.com
*  
*  2015-01-04  修改为 pdo连接数据库
**/
class Dbbase{
	protected $_dbConf = null;
	protected $_conn = array('master' => null, "slaver" => null );
	
	public function __construct($dbArr){
		$this->_dbConf["master"]["_host"]	=	$dbArr["master"]['host'];
		$this->_dbConf["master"]["_database"]	=	$dbArr["master"]['database'];
		$this->_dbConf["master"]["_user"]	=	$dbArr["master"]['user'];
		$this->_dbConf["master"]["_pw"]	=	$dbArr["master"]['pw'];
		$this->_dbConf["master"]["_charset"]	=	$dbArr["master"]['charset'];
		
		$this->_dbConf["slaver"]["_host"]	=	$dbArr["slaver"]['host'];
		$this->_dbConf["slaver"]["_database"]	=	$dbArr["slaver"]['database'];
		$this->_dbConf["slaver"]["_user"]	=	$dbArr["slaver"]['user'];
		$this->_dbConf["slaver"]["_pw"]	=	$dbArr["slaver"]['pw'];
		$this->_dbConf["slaver"]["_charset"]	=	$dbArr["slaver"]['charset'];
	}
	/**
	* 连接所需的数据库
	*/
	function connect($masterOrSlaver){
		$dsn="mysql:host=".$this->_dbConf[$masterOrSlaver]["_host"].";dbname=".$this->_dbConf[$masterOrSlaver]["_database"];
		$user=$this->_dbConf[$masterOrSlaver]["_user"];
		$pass=$this->_dbConf[$masterOrSlaver]["_pw"];
		$dbh = new PDO($dsn, $user, $pass, array(
				  PDO::ATTR_PERSISTENT => true
				));
		$dbh->query("set names " . $this->_dbConf[$masterOrSlaver]["_charset"]);
		$this->_conn[$masterOrSlaver]=$dbh;
	}
	
	/**
	**
	**returnbool true/false
	*/
	public function del($sql){
		if( !$this->_conn["master"] )$this->connect("master");
		
		$dbh = $this->getConn("master");
		$sth = $dbh->prepare($sql);
		$sth->execute();
		
		return $sth->rowCount();
	}
	/**
	**途薷菘械一
	**return bool true/false
	*/
	public function update($sql){
		if( !$this->_conn["master"] )$this->connect("master");
		
		$dbh = $this->getConn("master");
		$sth = $dbh->prepare($sql);
		$sth->execute();
		
		return $sth->rowCount();
	}
	/**
	**途菘胁一
	**return lastInsertId / false
	*/
	public function insert($sql){
		if( !$this->_conn["master"] )$this->connect("master");
		
		$dbh = $this->getConn("master");
		$sth = $dbh->prepare($sql);
		$sth->execute();
		
		return $dbh->lastInsertId();
	}
	/**
	**途菘选一
	**return  1D array
	*/
	
	public function fetchRow($sql){
		if( !$this->_conn["slaver"] )$this->connect("slaver");
		
		$dbh = $this->getConn("slaver");
		$sth = $dbh->prepare($sql);
		$sth->setFetchMode(PDO::FETCH_ASSOC);
		$sth->execute();
		$result = $sth->fetch();
		if($result){
			return $result;
		}
		return false;
	}
	
	/**
	**途菘选
	**return nD array
	*/		
	public function fetchAll($sql){
		if( !$this->_conn["slaver"] )$this->connect("slaver");
		
		$dbh = $this->getConn("slaver");
		$sth = $dbh->prepare($sql);
		$sth->setFetchMode(PDO::FETCH_ASSOC);
		if( $sth->execute() ){
			while ($row = $sth->fetch())
			{
				$output[] = $row;
			}
			if($sth->rowCount())
				return $output;
			else 
				return 0;
		}
		return false;
	}
	
	/**
	**鏌ヨ鏁伴噺
	**pram $table()
	**return int
	*/
	public function getTotal($table,$filter=null){

		$return = 0;
		$sql="select count(*) as num from `".$table."` ";
		if($filter){
			$sql.="WHERE ".$filter." ";
		}
		if( $row = $this->fetchRow($sql) ) $return = $row["num"];

		return $return;
	}
		
	/**
	**乇菘
	*/
	public function getConn($masterOrSlaver){
		return $this->_conn[$masterOrSlaver];
	}
	
	
	public function __distruct(){
		if($this->_conn["master"])$this->_conn["master"]=null;
		if($this->_conn["slaver"])$this->_conn["slaver"]=null;
	}
}